<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@hrbjjhh.com> <jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 城市模型
 */
class CityModel extends BaseModel {
	
	/* 自动验证规则 */
	protected $_validate = array(
			//array('ward_name', 'require', '行政区不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
			array('city_name', 'require', '请输入城市', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
			//array('city_id', 'require', '请选择城市', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
			array('province_id', 'require', '请选择省份', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
			array('city_name', 'checkName', '省份已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
			array('city_name', '1,20', '城市名称长度不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
	);
	
	/* 自动完成规则 */
	protected $_auto = array(
			array('create_time', NOW_TIME, self::MODEL_INSERT),
			array('update_time', NOW_TIME, self::MODEL_UPDATE),
			//array('province_id','1',self::MODEL_INSERT),
			//array('status', '1', self::MODEL_INSERT),
	);
	
	
	/**
	 * @todo 根据城市id取出城市名称
	 * @param int $city_id 城市id
	 * @param string $field 查询字段
	 * @return false|array
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getCity($city_id = 0,$field = ''){
		//获取数据
		$city_id = (int)$city_id;
		if($city_id == 0){
			$this->error = '请正确操作';
			return false;
		}
		$map = array();//初始化查询条件
		//查询条件
		$map['city_id'] = $city_id;
		$map['status'] = array('gt',-1);
		//返回值
		return $this->where($map)->field($field)->find();
	}
	
	/**
	 * @todo 检测名称是否存在
	 * @param  string $login_name 用户名
	 * @return boolean          ture - 未重名，false - 已重名
	 */
	protected function checkName($city_name){
		$city_id = (int)I('city_id');
		//判断是否存在
		$map = array();
		if($city_name != 0){
			$map['city_id'] = array('neq',$city_id);
		}
		$map['city_name'] = $city_name;
		$map['status'] = array('gt',-1);
		if($this->where($map)->find()){
			return false;
		}
		return true;
	}
	
	/**
	 * @todo 根据省份id取出城市名称
	 * @param int $province_id 省份id
	 * @param string $field 查询字段
	 * @return false|array
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getCityName($province_id = 0,$field = 'city_name'){
		//获取数据
		$province_id = (int)$province_id;
		if($province_id == 0){
			$this->error = '请正确操作';
			return false;
		}
		$map = array();//初始化查询条件
		//查询条件
		$map['province_id'] = $province_id;
		$map['status'] = array('gt',-1);
		//返回值
		return $this->where($map)->field($field)->select();
	}
	
	/**
	 * @todo 删除验证调取城市列表
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getCityList($map = array(),$field='',$orderby='city_id desc'){
		return $this->where($map)->field($field)->order($orderby)->select();
	}
	
	/**
	 * @todo 判断省份下是否有城市数据
	 * @param string $province_id
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function judgeProvince($province_id=''){
		if($province_id == ''){
			$this->error='参数丢失，请重试';
			return false;
		}
		$map['province_id'] = array('in',$province_id);
		$map['status'] = array('gt',-1);
		$city_list = $this->getCityList($map,'city_id');
		if(!empty($city_list)){
			$this->error = '省份下有城市，请先删除城市';
			return false;
		}
		return true;
	}
	
	/**
	 * @todo 返回主键
	 * @see \Think\Model::getPk()
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function getPk(){
		return 'city_id';
	}
}
